package com.aube.interact.taste.goods;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aube.interact.taste.R;
import com.aube.interact.taste.roundedimageview.RoundedImageView;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.dynamicload.video.model.GoodsModel;
import com.huyn.baseframework.utils.Utils;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by hill on 16/11/8.
 */


/**
 * 播放视频中出现的商品缩略图View
 */
public class SmallGoodsItem extends RelativeLayout {
    private String TAG = "SmallGoodsItem";

    private Context mContext;

    private RelativeLayout mRoot;
    private TextView mGoodsTitle;
    private RoundedImageView mGoodsImg;

    private String mImgUrl;

    private int mGoodsTitleWidth;

    private AtomicBoolean mAnimating = new AtomicBoolean(false);

    public SmallGoodsItem(Context context) {
        this(context, null);
    }

    public SmallGoodsItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SmallGoodsItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();
    }

    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.aube_intera_layout_smallgoodsitem, this);

        mRoot = (RelativeLayout) findViewById(R.id.smallgoodsitem_root);
        mGoodsTitle = (TextView) findViewById(R.id.smallgoodsitem_txt);
        mGoodsImg = (RoundedImageView) findViewById(R.id.smallgoodsitem_img);
        mGoodsImg.setBackground(getResources().getDrawable(R.drawable.bg_goodstag));

        mGoodsTitleWidth = Utils.dip2px(mContext, 180);
    }

    public void fillData(GoodsModel data) {
        if(data.goodsVo == null)
            return;

        mGoodsTitle.setText(data.goodsVo.tlTitle);

        mImgUrl = data.goodsVo.picurl;
        if(mImgUrl == null || mImgUrl.isEmpty())
            return;

        ImageLoader.getInstance(getContext()).fillImage(mGoodsImg, mImgUrl, 100, 100);
    }

    public int getGoodsImgSize() {
        return mGoodsImg.getMeasuredWidth();
    }

    public int[] getGoodsImgLocation() {
        int[] location = new int[2];
        mGoodsImg.getLocationInWindow(location);
        return location;
    }

    /**
     * 商品标题渐现出来
     */
    public void alphaInTitle() {
        setVisibility(View.VISIBLE);
        ObjectAnimator anim = ObjectAnimator.ofFloat(mGoodsTitle, "alpha", 0f, 1f);
        anim.setDuration(300);
        anim.start();
    }

    /**
     * 商品标题渐隐消失
     */
    public void alphaOutTitle(final AnimatorListenerAdapter adapter) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(mGoodsTitle, "alpha", 1f, 0f);
        anim.setDuration(300);
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                setVisibility(GONE);
                adapter.onAnimationEnd(null);
            }
        });
        anim.start();
    }

    /**
     * 商品图片渐现的动画, 渐现完毕, 商品名字从左往右横移动画
     */
    public void startAlphaInAnim() {
        Utils.Log(Utils.LogType.DEBUG, TAG, "startAlphaInAnim");
        mAnimating.set(true);

        ValueAnimator anim = ValueAnimator.ofFloat(0.0f, 1.0f);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float animValue = (float) animation.getAnimatedValue();

                mGoodsImg.setAlpha(animValue);
            }
        });

        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                startGoodsTextExtendAnim();
            }
        });

        anim.setDuration(300);
        anim.setTarget(mRoot);
        anim.start();

        Utils.Log(Utils.LogType.DEBUG, TAG, "startAlphaInAnim done");
    }


    /**
     * 商品名字从做至右的动画
     */
    private void startGoodsTextExtendAnim() {
        mGoodsTitle.setVisibility(VISIBLE);

        ValueAnimator anim = ValueAnimator.ofFloat(0.0f, 1.0f);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float animValue = (float) animation.getAnimatedValue();

                mGoodsTitle.setTranslationX(mGoodsTitleWidth * (animValue - 1));
            }
        });

        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mAnimating.set(false);
            }
        });

        anim.setDuration(400);
        anim.setTarget(mRoot);
        anim.start();
    }

    /**
     * 商品图片渐隐的动画
     */
    private void startAlphaOutAnim() {
        Utils.Log(Utils.LogType.DEBUG, TAG, "startAlphaInAnim");

        final ValueAnimator anim = ValueAnimator.ofFloat(1.0f, 0.0f);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float animValue = (float) animation.getAnimatedValue();

                mGoodsImg.setAlpha(animValue);
            }
        });

        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                setVisibility(GONE);
                mAnimating.set(false);
            }
        });

        anim.setDuration(300);
        anim.setTarget(mRoot);
        anim.start();

        Utils.Log(Utils.LogType.DEBUG, TAG, "startAlphaInAnim done");
    }

    /**
     * 商品名字从右至左的动画, 移动完毕, 商品图片渐隐消失
     */
    public void startGoodsTextCollapseAnim() {
        mAnimating.set(true);
        ValueAnimator anim = ValueAnimator.ofFloat(1.0f, 0.0f);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float animValue = (float) animation.getAnimatedValue();

                mGoodsTitle.setTranslationX(mGoodsTitleWidth * (animValue - 1));
            }
        });

        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                mGoodsTitle.setVisibility(GONE);
                startAlphaOutAnim();
            }
        });

        anim.setDuration(400);
        anim.setTarget(mRoot);
        anim.start();
    }

    public boolean IsAnimating() {
        return mAnimating.get();
    }

}
